import { 战利品, ItemData } from "./Datas/Data";
import { ItemType, SuppliesType } from "./Framework/Const/Constant";
import { BundleManager, Bundles } from "./Framework/Managers/BundleManager";
import { DataManager } from "./Framework/Managers/DataManager";
import NodeUtil from "./Framework/Utils/NodeUtil";
import { ResourceUtil } from "./Framework/Utils/ResourceUtil";
import { Tween } from "./Framework/Utils/Tween";
import { ZTool } from "./Framework/Utils/ZTool";

const { ccclass, property } = cc._decorator;

@ccclass
export default class DroppedItem extends cc.Component {

    Icon: cc.Sprite = null;
    collider: cc.PhysicsBoxCollider = null;

    goodsDatas: 战利品[] = [];

    itemData: any = null;

    @property
    generateGun: boolean = false;

    onLoad() {
        this.Icon = NodeUtil.GetComponent("Icon", this.node, cc.Sprite);
        this.collider = this.node.getComponent(cc.PhysicsBoxCollider);

        if (this.generateGun) {
            let type = ItemData.GunTypes[ZTool.GetRandomInt(0, ItemData.GunTypes.length)];
            let data = ZTool.DeepCopy(DataManager.ItemDatas.get(type)[ZTool.GetRandomInt(0, DataManager.ItemDatas.get(type).length)]);
            data.BulletCount = 500;
            this.Init(data);

            // let data = ZTool.DeepCopy(DataManager.itemDatas.get(ItemType.冲锋枪).find(e => e.Name == "MAC10"));
            // data.BulletCount = 500;
            // this.Init(data);
        }
    }

    Init(data) {
        cc.tween(this.node).to(1, { position: cc.v3(this.node.position.x, this.node.position.y + 30) })
            .to(1, { position: cc.v3(this.node.position.x, this.node.position.y) }).union().repeatForever().start();

        this.itemData = data;

        let scale = 1;
        if (data.Type == ItemType[ItemType.头盔]) scale = 0.6;
        if (data.Type == ItemType[ItemType.防弹衣]) scale = 0.8;
        if (data.Type == ItemType[ItemType.手枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.突击步枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.冲锋枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.射手步枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.栓动步枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.轻机枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.霰弹枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.卡宾枪]) scale = 0.8;
        if (data.Type == ItemType[ItemType.子弹]) scale = 0.6;
        if (data.Type == ItemType[ItemType.战利品]) scale = 0.6;

        this.Icon.node.setScale(scale);

        if (data) {
            BundleManager.LoadSpriteFrame(Bundles.Item, `${data.Type}_${data.Name}`).then((sf: cc.SpriteFrame) => {
                this.Icon.spriteFrame = sf;
            })
        }
    }

}